<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use app\common\library\helper;
use app\common\model\Category as CategoryModel;
use think\response\Json;
use app\api\model\{Goods as GoodsModel};
use app\api\service\{Goods as GoodsService};

/**
 * 商品控制器
 * Class Goods
 * @package app\api\controller
 */
class Goods extends Controller
{
    /**
     * 商品列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function list(): \think\response\Json
    {
        // 获取列表数据
        $model = new GoodsModel;
        $param = $this->request->param();
        $good_type = '';
        $listRows = 15;
        if (isset($param['categoryId'])){
            $categoryModel     = new CategoryModel();
            $parentIds = array_unique($categoryModel->getTopParentIds(array($param['categoryId'])));
            if(!empty($parentIds) && in_array('10042',$parentIds)){//虚拟商品不分页
                $listRows = 50;
                $good_type = 'virtual_good';
            }
        }
        $list = $model->getList($param,$listRows);
        foreach ($list as $goods) {
            /*if($goods['goods_label'] && is_string($goods['goods_label'])){
                $goods['goods_label'] = explode(',',$goods['goods_label']);
            }else{
                $goods['goods_label'] = [];
            }*/
            $goods['goods_label'] = [];
            if ($goods['goods_price_min']){
                $goods['goods_coupon_price'] = helper::bcmul($goods['goods_price_min'],0.5);
                $goods['goods_gift_price'] = helper::bcmul($goods['goods_price_min'],0.75);
            }
        }
        return $this->renderSuccess(compact('list','good_type'));
    }

    /**
     * 获取商品详情
     * @param int $goodsId
     * @return \think\response\Json
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(int $goodsId): \think\response\Json
    {
        // 商品详情
        $model = new GoodsModel;
        $goodsInfo = $model->getDetails($goodsId);
        /*if($goodsInfo['goods_label'] && is_string($goodsInfo['goods_label'])){
            $goodsInfo['goods_label'] = explode(',',$goodsInfo['goods_label']);
        }else{
            $goodsInfo['goods_label'] = [];
        }*/
        $goodsInfo['goods_label'] = [];
        if ($goodsInfo['goods_price_min']){
            $goodsInfo['goods_coupon_price'] = helper::bcmul($goodsInfo['goods_price_min'],0.5);
            $goodsInfo['goods_gift_price'] = helper::bcmul($goodsInfo['goods_price_min'],0.75);
        }
        return $this->renderSuccess(['detail' => $goodsInfo]);
    }

    /**
     * 获取商品详情(基础信息)
     * @param int $goodsId
     * @param bool $verifyStatus
     * @return \think\response\Json
     * @throws \cores\exception\BaseException
     */
    public function basic(int $goodsId, bool $verifyStatus = true): \think\response\Json
    {
        // 获取商品详情
        $model = new GoodsModel;
        $detail = $model->getBasic($goodsId, $verifyStatus);
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 获取商品规格数据
     * @param int $goodsId 商品ID
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function specData(int $goodsId): \think\response\Json
    {
        // 获取商品详情
        $model = new GoodsModel;
        $specData = $model->getSpecData($goodsId);
        return $this->renderSuccess(compact('specData'));
    }

    /**
     * 获取商品的指定SKU信息
     * @param int $goodsId 商品ID
     * @param string $goodsSkuId 商品SKU标识
     * @return Json
     */
    public function skuInfo(int $goodsId, string $goodsSkuId): Json
    {
        $skuInfo = GoodsService::getSkuInfo($goodsId, $goodsSkuId);
        return $this->renderSuccess(compact('skuInfo'));
    }
}
